package com.ctsi.organise.api.impl.local;

import com.ctsi.commons.util.UtilValidate;
import com.ctsi.commons.util.response.Page;
import com.ctsi.organise.common.api.UserLoginServiceProxy;
import com.ctsi.organise.common.model.Person;
import com.ctsi.organise.common.model.UserLogin;
import com.ctsi.organise.service.UserLoginEntityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author: zhangjw
 * @Date: 2018/11/13 04:21
 * @Description:
 */
@Service("UserLoginServiceProxy")
@Primary
public class UserLoginServiceProxyImpl implements UserLoginServiceProxy {

    @Autowired
    private UserLoginEntityManager userLoginEntityManager;
    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 根据登陆名找到用户
     *
     * @param loginName
     * @return
     */
    @Override
    public UserLogin findUserLoginByLoginName(String loginName) {
        return userLoginEntityManager.findUserLoginByLoginName(loginName);
    }

    @Override
    public List<UserLogin> findUserLoginByPartyid(String partyId) {
        throw new RuntimeException("method not implemented!!!");
    }

    @Override
    public Person getUserInfoByLoginId(String logInId, String partyId) {
        throw new RuntimeException("method not implemented!!!");
    }

    /**
     * 根据用户名和密码查询用户是否存在
     *
     * @param loginName
     * @param password
     * @return
     */
    @Override
    public UserLogin findUserLoginByAccandPwd(String loginName, String password) {
        if (UtilValidate.isEmpty(loginName) || UtilValidate.isEmpty(password)) {
            return null;
        }
        UserLogin userlogin = this.findUserLoginByLoginName(loginName);
        if (null == userlogin) {
            return null;
        } else {

          return  passwordEncoder.matches(password ,userlogin.getPassword()) ? userlogin : null;
        }
    }

    /**
     * 根据用户登录id获取用户登录信息
     *
     * @param loginId
     * @return
     */
    @Override
    public UserLogin findById(String loginId) {
        return userLoginEntityManager.findUserLoginEntityById(loginId);
    }

    /**
     * 修改登录用户信息
     *
     * @param user
     */
    @Override
    public void updateUserLogin(UserLogin user) {
        throw new RuntimeException("method not implemented!!!");
    }

    /**
     * 校验用户密码
     *
     * @param user
     * @param pwd
     * @return
     */
    @Override
    public boolean validatePass(String user, String pwd) {
        throw new RuntimeException("method not implemented!!!");
    }

    /**
     * @param partyIds
     * @return
     */
    @Override
    public List<UserLogin> findUserLoginByPartyIds(List<String> partyIds) {
        throw new RuntimeException("method not implemented!!!");
    }

    /**
     * 加入内部用户
     *
     * @param user
     */
    @Override
    public void insertInternalUser(UserLogin user) {
        throw new RuntimeException("method not implemented!!!");
    }

    /**
     * 删除登录用户
     *
     * @param user 可以根据id或者loginName或者partyid删除，三者选其一
     */
    @Override
    public void delUserLogin(UserLogin user) {
        throw new RuntimeException("method not implemented!!!");
    }

    /**
     * 修改登录用户密码
     *
     * @param loginName
     * @param oldPassword
     * @param newPassword
     */
    @Override
    public void modifyPassword(String loginName, String oldPassword, String newPassword) {
        throw new RuntimeException("method not implemented!!!");
    }

    /**
     * 返回登录用户 支持分页
     *
     * @param userName
     * @param action
     * @param map
     * @param startIndex
     * @param pageSize
     * @return
     */
    @Override
    public Page<UserLogin> findUserLoginByPage(String userName, String action, Map map, int startIndex, int pageSize) {
        throw new RuntimeException("method not implemented!!!");
    }
}
